package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.PageResult;
import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.dto.ProductReviewRequest;
import cn.edu.ncut.cs.springboot.petmanagementsystem.dto.ServiceReviewRequest;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.ProductReview;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.ServiceReview;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 评价控制器
 */
@Slf4j
@RestController
@RequestMapping("/review")
@Tag(name = "评价管理")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 创建商品评价
     */
    @Operation(summary = "创建商品评价")
    @PostMapping("/product")
    public Result<ProductReview> createProductReview(
            @Valid @RequestBody ProductReviewRequest request) {
        try {
            // 获取当前登录用户信息
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();
            Long userId = Long.parseLong(authentication.getCredentials().toString());

            ProductReview review = reviewService.createProductReview(userId, username, request);
            return Result.success("商品评价成功", review);
        } catch (Exception e) {
            log.error("创建商品评价失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取商品评价列表
     */
    @Operation(summary = "获取商品评价列表")
    @GetMapping("/product/{productId}")
    public Result<PageResult<ProductReview>> getProductReviews(
            @Parameter(description = "商品ID") @PathVariable Long productId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageResult<ProductReview> result = reviewService.getProductReviews(productId, page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取商品评价列表失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建服务评价
     */
    @Operation(summary = "创建服务评价")
    @PostMapping("/service")
    public Result<ServiceReview> createServiceReview(
            @Valid @RequestBody ServiceReviewRequest request) {
        try {
            // 获取当前登录用户信息
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();
            Long userId = Long.parseLong(authentication.getCredentials().toString());

            ServiceReview review = reviewService.createServiceReview(userId, username, request);
            return Result.success("服务评价成功", review);
        } catch (Exception e) {
            log.error("创建服务评价失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取服务评价列表
     */
    @Operation(summary = "获取服务评价列表")
    @GetMapping("/service/{serviceTypeId}")
    public Result<PageResult<ServiceReview>> getServiceReviews(
            @Parameter(description = "服务类型ID") @PathVariable Long serviceTypeId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageResult<ServiceReview> result = reviewService.getServiceReviews(serviceTypeId, page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取服务评价列表失败", e);
            return Result.error(e.getMessage());
        }
    }
}
